/**
 * Created by Anard on 2017/4/17.
 */
var mysql = require('./mysql.js');

module.exports = publicDao;

function publicDao() {
    this.c = {};
    this.c.table = '';
    this.c.wheres = [];
    this.c.args = [];
    this.c.pluck = '*';
    this.c.orders = [];
    this.c.limit = '';
    this.c.values = [];

    return this;
}

publicDao.prototype.table = function(table){
    this.c.table = table;

    return this;
};

publicDao.prototype.pluck = function(){
    var ar = [];
    for (var i in arguments) {
        ar.push(arguments[i]);
    }
    this.c.pluck = '`' + ar.join('`,`') + '`';

    return this;
};

publicDao.prototype.pluck = function(num, start){
    if (!start && start !== 0) {
        this.c.limit = 'limit ' + num;
    } else {
        this.c.limit = 'limit ' + start + ',' + num;
    }

    return this;
};

publicDao.prototype.wheres = function(field, type, value){
    this.c.wheres.push('`' + field + '` ' + type + ' ?');
    this.c.args.push(value);

    return this;
};

publicDao.prototype.orders = function (field, type) {
    if (type === 0 || type == 'asc' || type == 'ASC') {
        this.c.orders.push('`' + field + '` ASC');
    } else {
        this.c.orders.push('`' + field + '` DESC');
    }

    return this;
};

publicDao.prototype.mixWheres = function () {
    if (this.c.wheres.length > 0) {
        return this.c.wheres.join(' AND ');
    } else {
        return 1;
    }
};

publicDao.prototype.mixOrders = function () {
    if (this.c.orders.length > 0) {
        return 'ORDER BY ' + this.c.orders.join(' , ');
    } else {
        return '';
    }
};

publicDao.prototype.get = function (cb) {
    var sql = "SELECT " + this.c.pluck + " FROM `" + this.c.table + "` WHERE " + this.mixWheres() + " " + this.mixOrders() + " " + this.c.limit;
    var args = this.c.args;
    //console.error(sql);
    //console.error(args);
    mysql.query(sql, args, function (err, res) {
        if (err !== null) {
            cb({code: err.number, msg: err.message}, null);
        } else {
            //console.error(res);
            cb(null, res);
        }
    });
};

publicDao.prototype.values = function (key, value) {
    this.c.values.push({'key': key, 'value': value});

    return this;
};

publicDao.prototype.insert = function (cb) {
    var field = [];
    var value = [];
    var args = [];
    for (var i in this.c.values) {
        var v = this.c.values[i];
        field.push(v.key);
        value.push('?');
        args.push(v.value);
    }
    var sql = "INSERT INTO " + this.c.table + " (" + field.join(',') + ") VALUES (" + value.join(',') + ")";
    //console.error(sql);
    //console.error(args);
    mysql.insert(sql, args, function (err, res) {
        if (err !== null) {
            cb({code: err.number, msg: err.message}, null);
        } else {
            //console.error(res);
            cb(null, res);
        }
    });
};

publicDao.prototype.update = function (cb) {
    var value = [];
    var args = [];
    for (var i in this.c.values) {
        var v = this.c.values[i];
        value.push(v.key + '=?');
        args.push(v.value);
    }
    var sql = "UPDATE `" + this.c.table + "` SET " + value.join(',') + " WHERE " + this.mixWheres();
    args = args.concat(this.c.args);
    //console.error(sql);
    //console.error(args);
    mysql.update(sql, args, function (err, res) {
        if (err !== null) {
            cb({code: err.number, msg: err.message}, null);
        } else {
            //console.error(res);
            cb(null, res);
        }
    });
};

publicDao.prototype.delete = function (cb) {
    var sql = "DELETE FROM `" + this.c.table + "` WHERE " + this.mixWheres();
    var args = this.c.args;
    //console.error(sql);
    //console.error(args);
    mysql.delete(sql, args, function (err, res) {
        if (err !== null) {
            cb({code: err.number, msg: err.message}, null);
        } else {
            //console.error(res);
            cb(null, res);
        }
    });
};